package com.itheima.tlias.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.tlias.mapper.EmpExprMapper;
import com.itheima.tlias.mapper.EmpMapper;
import com.itheima.tlias.pojo.*;
import com.itheima.tlias.service.EmpLogService;
import com.itheima.tlias.service.EmpService;
import com.itheima.tlias.utils.JwtUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
@Slf4j
public class EmpServiceImpl implements EmpService {

	@Autowired
	private EmpMapper empMapper;

	@Autowired
	private EmpExprMapper empExprMapper;

	@Autowired
	private EmpLogService empLogService;

	@Override
	public PageResult<Emp> page(String name, Integer gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
		//获取总员工表
		PageHelper.startPage(page, pageSize);

		Page<Emp> empPage = (Page<Emp>) empMapper.list(name, gender, begin, end);

		return new PageResult<>(empPage.getTotal(), empPage.getResult());
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void save(Emp emp) throws Exception {
		try {
			//保存员工信息
			emp.setUpdateTime(LocalDateTime.now());
			emp.setCreateTime(LocalDateTime.now());
			empMapper.save(emp);

			//int i = 1 / 0;
			//if (true) {
			//	throw new Exception("报错了");
			//}

			//保存工作经历
			Integer empId = emp.getId();
			List<EmpExpr> exprsList = emp.getExprList();
			if (!CollectionUtil.isEmpty(exprsList)) {
				exprsList.forEach(empExpr -> empExpr.setEmpId(empId));
				empExprMapper.insertBatch(exprsList);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		} finally {
			EmpLog empLog = new EmpLog(null, LocalDateTime.now(), emp.toString());
			empLogService.insertLog(empLog);
		}
	}

	@Override
	public List<Emp> listAll() {
		return empMapper.listAll();
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void deleteByIds(List<Integer> ids) {
		empMapper.deleteByIds(ids);


		empExprMapper.deleteByEmpIds(ids);
	}

	@Override
	public Emp infoById(Integer id) {
		long threadId = Thread.currentThread().getId();
		log.info("在Service中的线程id为：{}", threadId);
		return empMapper.infoById(id);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void update(Emp emp) {
		//跟新员工信息
		emp.setUpdateTime(LocalDateTime.now());
		empMapper.update(emp);

		empExprMapper.deleteByEmpIds(Collections.singletonList(emp.getId()));

		List<EmpExpr> exprsList = emp.getExprList();
		if (!CollectionUtil.isEmpty(exprsList)) {
			exprsList.forEach(empExpr -> empExpr.setEmpId(emp.getId()));
			empExprMapper.insertBatch(exprsList);
		}


	}

	@Override
	public JobDateOption getJobData() {
		List<Map<String, Object>> list = empMapper.getJobData();
		List<Object> jobList = list.stream().map(pos -> pos.get("pos")).toList();
		List<Object> dataList = list.stream().map(pos -> pos.get("total")).toList();
		return new JobDateOption(jobList, dataList);
	}

	@Override
	public List<Map<String, Object>> getGenderData() {
		//List<Map<String, Object>> list = empMapper.getGenderData();
		return empMapper.getGenderData();
	}

	@Override
	public LogInfo login(Emp emp) {
		Emp empDB = empMapper.getEmpByUsernameAndPassword(emp);
		if (empDB != null) {
			Map<String, Object> map = new HashMap<>();
			map.put("id", empDB.getId());
			map.put("username", empDB.getUsername());
			String token = JwtUtils.generateJwt(map);
			return new LogInfo(empDB.getId(), empDB.getName(), empDB.getUsername(), token);
		}
		return null;
	}


}
